Swift 2.2 新特性NOTE

Swift 2.2 新特性NOTE

慕课网:http://www.imooc.com/learn/750

1. 弃用 ++,–运算符

var a = 3
//a++
a += 1
//a--
a -= 1                                        

为什么取消

  1. –a ; a– 容易让人迷惑
  2. += 更符合语意

2. Deprecate-C-Style-For-Loop

// 使用区间运算符
for i in 1...10{
    print(i)
}

for i in 1..<10{
    print(i)
}

// 反向遍历可以使用reversed
for i in (1...10).reversed(){
    print(i)
}

for i in (1..<10).reversed(){
    print(i)
}
// 使用stride to表示不包括类似于区间 ..<
for i in stride(from:0, to:10, by:2){
    print(i)
}
//through表示包含 类似于区间 ...
for i in stride(from:0, through:10, by:2){
    print(i)
}
//递减 步长为-2
for i in stride(from:10, to:0, by:-2){
    print(i)
}
//使用浮点数
for i in stride(from:0.5, to:1.5, by:0.1){
    print(i)
}

3.Tuple的比较, Deprecate-Tuple-Splat(没用过)

// tuple 的比较
let score1 = (chinese:90, math:95)
let score2 = (chinese:90, math:100)
let score3 = (chinese:100, math:90)

score1 < score2  //ture
score3 < score2 //false

4. Selectors-Can-Not-Be-a-String

//以前使用字符串,容易出现拼写错误
   //button.addTarget(self, action: "click", for: .touchUpInside)
    button.addTarget(self, action: #selector(click), for: .touchUpInside)